//
//  MyBoard.m
//  Bespoke
//
//  Created by Cheng on 12/7/18.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MyBoardCell.h"
#import "AccountMyBoard.h"
#import "AccountBoard.h"
#import "MyBoardUIViewController.h"
#import "Split.h"

@implementation MyBoardCell
@synthesize Lab_Title, TF_Edit, Bt_Edit, Bt_Share, Bt_Mybook;
@synthesize IVw_Media_main, IVw_Media_second, IVw_Media_third, IVw_Media_fourth;
@synthesize Lab_Media_main, Lab_Media_second, Lab_Media_third, Lab_Media_fourth;
@synthesize AB_CurrentBook;

- (void) updateBoardInfo {
    
    int order = 0;
    
    [Lab_Title setFont:[UIFont fontWithName:@"EamesCenturyModern-Regular" size:13]];
    Lab_Title.text = self.AB_CurrentBook.name;
    
    for (MediaListsMedia *Medias in self.AB_CurrentBook.MediaArray) {
        
        order++;
        NSURL *url;
        
        if (Medias.imageArray.count != 0) {
            
            MediaListsImage *images = [Medias.imageArray objectAtIndex:0];
            url = [NSURL URLWithString:[NSString stringWithString:images.image_filename]];
            [self SetImageLocation:url SetOrder:order];
        }
        else if ((![Medias.MLH_html.NS_ImageName isEqualToString:@""])&&(Medias.MLH_html.NS_ImageName!=nil)) {
            
            url = [NSURL URLWithString:[NSString stringWithString:Medias.MLH_html.NS_ImageName]];
            [self SetImageLocation:url SetOrder:order];
        }
        else {
            NSString *NS_Text_Temp;
            
            if((![Medias.text.text_title isEqualToString:@""])&&(Medias.text.text_title!=nil))
                NS_Text_Temp = [Medias.text.text_title TitleSplitPressed];
            else if((![Medias.MLH_html.NS_htmlTitle isEqualToString:@""])&&(Medias.MLH_html.NS_htmlTitle!=nil))
                NS_Text_Temp = [Medias.MLH_html.NS_htmlTitle TitleSplitPressed];
            else if((![Medias.text.text_description isEqualToString:@""])&&(Medias.text.text_description!=nil))
                NS_Text_Temp = [Medias.text.text_description TitleSplitPressed];
            
            switch (order) {
                case 1:
                    Lab_Media_main.text = NS_Text_Temp;
                    Lab_Media_main.hidden = NO;
                    break;
                    
                case 2:
                    Lab_Media_second.text = NS_Text_Temp;
                    Lab_Media_second.hidden = NO;
                    break;
                    
                case 3:
                    Lab_Media_third.text = NS_Text_Temp;
                    Lab_Media_third.hidden = NO;
                    break;
                    
                case 4:
                    Lab_Media_fourth.text = NS_Text_Temp;
                    Lab_Media_fourth.hidden = NO;
                    break;
            }
        }
    }
}



#pragma mark - SetImageLocation Function

- (void)SetImageLocation:(NSURL *)url SetOrder:(int)order {
    
//    NSMutableArray *Array_ShareGroup = [[NSMutableArray alloc] init];
//
//    if ([AB_CurrentBook.ShareGroup rangeOfString:@","].location == NSNotFound)
//        [Array_ShareGroup addObject:AB_CurrentBook.ShareGroup];
//    else
//        [Array_ShareGroup addObjectsFromArray:[AB_CurrentBook.ShareGroup componentsSeparatedByString:@","]];
//    
//    NSLog(@"%d", Array_ShareGroup.count);
//    if(Array_ShareGroup.count != 0){
//        [Bt_Share setImage:[UIImage imageNamed:@"share_circle.png"] forState:UIControlStateNormal];
//    }
    
    //Set Activity.
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityIndicator.hidesWhenStopped = YES;
    activityIndicator.hidden = NO;
    [activityIndicator startAnimating];
    __weak MyBoardCell *Temp_self = self;
    
    switch (order) {
        case 1: {
            activityIndicator.center = CGPointMake(IVw_Media_main.frame.size.width /2, IVw_Media_main.frame.size.height/2);
            [IVw_Media_main setImageWithURL:url success:^(UIImage *image) {
                Temp_self.IVw_Media_main.image = [Temp_self.IVw_Media_main.image imageByScalingAndCroppingForSize:CGSizeMake(Temp_self.IVw_Media_main.frame.size.width,
                                                                                                                             Temp_self.IVw_Media_main.frame.size.height)];
                [activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
            } failure:^(NSError *error) {
                [activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
            }];
            [IVw_Media_main addSubview:activityIndicator];
            
            break;
        }
        case 2: {
            activityIndicator.center = CGPointMake(IVw_Media_second.frame.size.width /2, IVw_Media_second.frame.size.height/2);
            [IVw_Media_second setImageWithURL:url success:^(UIImage *image) {
                
                Temp_self.IVw_Media_second.image = [Temp_self.IVw_Media_second.image imageByScalingAndCroppingForSize:CGSizeMake(Temp_self.IVw_Media_second.frame.size.width,
                                                                                                                                 Temp_self.IVw_Media_second.frame.size.height)];
                [activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
            } failure:^(NSError *error) {
                [activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
            }];
            [IVw_Media_second addSubview:activityIndicator];
            
            break;
        }
        case 3: {
            activityIndicator.center = CGPointMake(IVw_Media_third.frame.size.width /2, IVw_Media_third.frame.size.height/2);
            [IVw_Media_third setImageWithURL:url success:^(UIImage *image) {
                
                Temp_self.IVw_Media_third.image = [Temp_self.IVw_Media_third.image imageByScalingAndCroppingForSize:CGSizeMake(Temp_self.IVw_Media_third.frame.size.width,
                                                                                                                               Temp_self.IVw_Media_third.frame.size.height)];
                [activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
            } failure:^(NSError *error) {
                [activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
            }];
            [IVw_Media_third addSubview:activityIndicator];
            
            break;
        }
        case 4: {
            activityIndicator.center = CGPointMake(IVw_Media_fourth.frame.size.width /2, IVw_Media_fourth.frame.size.height/2);
            [IVw_Media_fourth setImageWithURL:url success:^(UIImage *image) {
                
                Temp_self.IVw_Media_fourth.image = [Temp_self.IVw_Media_fourth.image imageByScalingAndCroppingForSize:CGSizeMake(Temp_self.IVw_Media_fourth.frame.size.width,
                                                                                                                                 Temp_self.IVw_Media_fourth.frame.size.height)];
                [activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
            } failure:^(NSError *error) {
                [activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
            }];
            [IVw_Media_fourth addSubview:activityIndicator];
            
            break;
        }
    }
}

@end
